package com.jz.thread;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class Task implements Delayed,Runnable{
	
	private long delayTime;
	
	

	public Task(long delayTime) {
		super();
		setDelayTime(delayTime);
	}

	public long getDelayTime() {
		return delayTime;
	}

	public void setDelayTime(long delayTime) {
		this.delayTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(delayTime, TimeUnit.MILLISECONDS);
		//this.delayTime = delayTime;//不能用这个时间
	}

	public long getDelay(TimeUnit unit) {
		if (delayTime == 0) return 0;
		return unit.convert(delayTime - System.nanoTime(), TimeUnit.NANOSECONDS);
	}

	public int compareTo(Delayed o) {
		Task that = (Task) o;
		if (this.delayTime < that.delayTime) {
			return -1;
		} else if (this.delayTime > that.delayTime) {
			return 1;
		} else {
			return   0;
		}
	}

	public void run() {
		System.out.println("delayTime" + delayTime + System.currentTimeMillis());
	}



}
